package com.zx.viewlearn.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.zx.viewlearn.R;

/**
 * 属性动画学习
 */
public class ViewLearnActivity2Animator extends AppCompatActivity {
    ImageView my_iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_learn2);
        my_iv = findViewById(R.id.my_iv);


    }


    /**
     * valueAnimator ofFloat
     *
     * @param view
     */
    public void valueAnimator1(View view) {
        ValueAnimator mValueAnimator = ValueAnimator.ofFloat(0, 1).setDuration(1000);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("ViewLearnActivity2", "value:" + value);
            }
        });
        mValueAnimator.start();
    }

    /**
     * valueAnimator  ofInt
     *
     * @param view
     */
    public void valueAnimator2(View view) {
        ValueAnimator mValueAnimator = ValueAnimator.ofInt(0, 10).setDuration(1000);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();

                Log.e("ViewLearnActivity2", animation.getRepeatCount() + "-----：value:" + value);
            }
        });
        mValueAnimator.start();
    }

    /**
     * valueAnimator  setStartDelay  setRepeatCount setRepeatMode
     *
     * @param view
     */
    public void valueAnimator3(View view) {
        ValueAnimator mValueAnimator = ValueAnimator.ofInt(0, 10).setDuration(1000);
//        mValueAnimator.setStartDelay(1000);//延时1秒
        mValueAnimator.setRepeatCount(3);//重复3次，一共会执行4次！
        mValueAnimator.setRepeatMode(ValueAnimator.REVERSE);//重复时倒叙执行
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Log.e("ViewLearnActivity2", "getTotalDuration:" + mValueAnimator.getTotalDuration());//总耗时
        }
        mValueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                int value = (int) ((ValueAnimator) animation).getAnimatedValue();
                Log.e("ViewLearnActivity2", "onAnimationStart-----：" + value);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                int value = (int) ((ValueAnimator) animation).getAnimatedValue();
                Log.e("ViewLearnActivity2", "onAnimationEnd-----：" + value);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                int value = (int) ((ValueAnimator) animation).getAnimatedValue();
                Log.e("ViewLearnActivity2", "onAnimationCancel-----：" + value);
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                int value = (int) ((ValueAnimator) animation).getAnimatedValue();
                Log.e("ViewLearnActivity2", "onAnimationRepeat-----：" + value);
            }
        });
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();

                Log.e("ViewLearnActivity2", animation.getRepeatCount() + "-----：value:" + value);
            }
        });
        mValueAnimator.start();
    }


    /**
     * objectAnimator ： ofFloat
     *
     * @param view
     */
    public void objectAnimator1(View view) {
//        ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(my_iv, "translationX", 0, 100).setDuration(1000);//平移
//        ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(my_iv, "translationY", 0, 100).setDuration(1000);//垂直移动
//        ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(my_iv, "alpha", 1, 0).setDuration(1000);//显隐
//        ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(my_iv, "rotation", 0, 360).setDuration(1000);//旋转
        ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(my_iv, "scaleY", 1, 3, 1, 0).setDuration(1000);//拉升
//                .ofFloat(my_iv, "translationY", 0, 100).setDuration(1000);
        mObjectAnimator.setRepeatCount(3);//重复3次，一共会执行4次！
        mObjectAnimator.setRepeatMode(ValueAnimator.REVERSE);//重复时倒叙执行
        mObjectAnimator.start();
    }


    /**
     * AnimatorSet
     *
     * @param view
     */
    public void animatorSet(View view) {
        ObjectAnimator mObjectAnimator1 = ObjectAnimator.ofFloat(my_iv, "translationX", 0, 100);//平移
        ObjectAnimator mObjectAnimator2 = ObjectAnimator.ofFloat(my_iv, "translationY", 0, 100);//垂直移动
        ObjectAnimator mObjectAnimator3 = ObjectAnimator.ofFloat(my_iv, "alpha", 0, 1);//显隐
        ObjectAnimator mObjectAnimator4 = ObjectAnimator.ofFloat(my_iv, "rotation", 0, 360);//旋转
        ObjectAnimator mObjectAnimator5 = ObjectAnimator.ofFloat(my_iv, "scaleY", 1, 3, 1);//拉升
        AnimatorSet mAnimatorSet = new AnimatorSet();
        mAnimatorSet.play(mObjectAnimator1).with(mObjectAnimator5).with(mObjectAnimator3).with(mObjectAnimator4).after(mObjectAnimator2);
        mAnimatorSet.setDuration(1000);
        mAnimatorSet.addListener(new AnimatorListenerAdapter() {
            /**
             * {@inheritDoc}
             *
             * @param animation
             */
            @Override
            public void onAnimationStart(Animator animation) {


                int value = ((AnimatorSet) animation).getChildAnimations().size();
                Log.e("ViewLearnActivity2", "onAnimationRepeat-----：" + value);
            }
        });
        mAnimatorSet.start();

    }
}
